using System;
using System.Collections.Generic;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Data;
using InfoExpress.WinApp.Utils;
using InfoExpress.HIV.Services;
using InfoExpress.HIV.WinApp.Utils;
using InfoExpress.HIV.Domains;

namespace InfoExpress.HIV.WinApp.Popups
{
  public partial class FrmThayDoiPhacDo : XtraForm
  {
    private int _benhNhanId = 0;
    private string _maBenhNhan = string.Empty;
    private TList<UtPhacdo> _phacDoList;

    public FrmThayDoiPhacDo()
    {
      InitializeComponent();
    }

    public FrmThayDoiPhacDo(int benhNhanId, string maBenhNhan)
      : this()
    {
      _benhNhanId = benhNhanId;
      _maBenhNhan = maBenhNhan;
    }

    #region Event Handlers

    private void FrmThayDoiPhacDo_Load(object sender, EventArgs e)
    {
      BindData();
      LoadData();
    }

    #region Command Handlers

    private void btnOK_Click(object sender, EventArgs e)
    {
      if (dxValidationProvider.Validate())
      {
        try
        {
          string maPhacDo = luePhacDoMoi.EditValue as string;
          int total = 0;
          GlobalCache.ThayDoiPhacDoService.GetPaged(
            string.Format("MaPhacDo='{0}' AND BenhNhanId={1}", maPhacDo, _benhNhanId),
            string.Empty, 0, 1, out total);

          if (total == 0)
          {
            ThayDoiPhacDo phacDo = new ThayDoiPhacDo();
            phacDo.BenhNhanId = _benhNhanId;
            phacDo.MaPhacDo = maPhacDo;
            phacDo.LyDo = meNote.Text;
            phacDo.NgayThayDoi = DateTime.Today;
            phacDo.UserName = GlobalCache.Permission.Username;

            GlobalCache.ThayDoiPhacDoService.Save(phacDo);

            MainFormUtils.SetSavedStatus();
            DialogResult = DialogResult.OK;
            Close();

            //For Non-Webservice
            //using (ConnectionScope.CreateTransaction())
            //{
            //  ThayDoiPhacDo phacDo = new ThayDoiPhacDo();
            //  phacDo.BenhNhanId = _benhNhanId;
            //  phacDo.MaPhacDo = maPhacDo;
            //  phacDo.LyDo = meNote.Text;

            //  GlobalCache.ThayDoiPhacDoService.Save(phacDo);

            //  if (ConnectionScope.Complete())
            //  {
            //    MainFormUtils.SetSavedStatus();
            //    DialogResult = DialogResult.OK;
            //    Close();
            //  }
            //  else
            //  {
            //    MainFormUtils.SetSavedFailedStatus();
            //    UIUtils.ShowErrorMessage(Constants.MsgErrSave);
            //  }
            //}
          }
        }
        catch (Exception ex)
        {
          Logger.LogCritical("FrmThayDoiPhacDo::btnOK_Click", ex, "General", null);
          UIUtils.ShowCannotSaveDataMessage();
        }
      }
    }

    #endregion

    #endregion

    #region Helper Methods

    private void BindData()
    {
      try
      {
        _phacDoList = GlobalCache.PhacDoService.GetAll();
        luePhacDoMoi.Properties.DataSource = _phacDoList;
      }
      catch (Exception ex)
      {
        MainFormUtils.SetLoadedFailedStatus();
        Logger.LogCritical("FrmThayDoiPhacDo::BindData", ex, "General", null);
        UIUtils.ShowCannotLoadDataMessage();
      }
    }

    private void LoadData()
    {
      try
      {
        DataSet dtsBenhNhan = 
          GlobalCache.BenhNhanDangKyService.LayThongTinDayDu(_benhNhanId);
        DataRow dataRow = dtsBenhNhan.Tables[0].Rows[0];

        txtMaThe.Text = _maBenhNhan;
        txtOPC1.EditValue = dataRow["MaDonVi"];
        txtOPC2.EditValue = dataRow["TenDonVi"];

        DataSet dtsPhacDo = 
          GlobalCache.ThayDoiPhacDoService.LayPhacDoCuoi(_benhNhanId);
        if (dtsPhacDo.Tables[0].Rows.Count > 0)
        {
          dataRow = dtsPhacDo.Tables[0].Rows[0];
          txtPhacDoCu1.EditValue = dataRow["MaPhacDo"];
          txtPhacDoCu2.EditValue = dataRow["DienGiai"];
          dePhacDoCu.EditValue = dataRow["NgayThayDoi"];
        }
      }
      catch (Exception ex)
      {
        Logger.LogCritical("FrmThayDoiPhacDo::LoadData", ex, "General", null);
        UIUtils.ShowCannotLoadDataMessage();
      }
    }

    #endregion

  }
}